//
//  ADView.m
//  UITableViewCell定制
//
//  Created by LZXuan on 14-5-22.
//  Copyright (c) 2014年 轩哥. All rights reserved.
//

#import "ADView.h"
#import "MyControl.h"
#import "UIImageView+WebCache.h"
@implementation ADView
{
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
    UILabel *_label;
}
- (void)dealloc{
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self showUI];
    }
    return self;
}


- (void)showUI{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 160)];
    for (int i = 0; i < 4; i++) {
        UIImageView *image = [MyControl creatImageViewWithFrame:CGRectMake(kScreenSize.width*i, 0, kScreenSize.width, 160) imageName:nil];
        [_scrollView addSubview:image];
    }
    _scrollView.contentSize = CGSizeMake(kScreenSize.width*4, 160);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 130, kScreenSize.width, 30)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.5;
    
    _label = [MyControl creatLabelWithFrame:CGRectMake(10, 0, 200, 30) text:@"图片0的标题"];
    _label.textColor = [UIColor whiteColor];
    [view addSubview:_label];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(200, 0, 120, 30)];
    _pageControl.numberOfPages = 4;
    //添加翻页触发事件
    [_pageControl addTarget:self action:@selector(pageControl:) forControlEvents:UIControlEventValueChanged];
    [view addSubview:_pageControl];
    [self addSubview:view];
    [view release];
}

- (void)loadImage:(NSString *)url {
    static NSInteger n = 0;
    if (n>3) {
        return;
    }
    [_scrollView.subviews[n++] sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
}

//控制滚动
- (void)pageControl:(UIPageControl *)page{
    [_scrollView setContentOffset:CGPointMake(_pageControl.currentPage*kScreenSize.width, 0) animated:YES];
    _label.text = [NSString stringWithFormat:@"图片%ld的标题",page.currentPage];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint point = scrollView.contentOffset;
    _pageControl.currentPage = point.x/kScreenSize.width;
    _label.text = [NSString stringWithFormat:@"图片%ld的标题",_pageControl.currentPage];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
